package servlet;

import dao.AttendanceDao;
import dao.CourseDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/add_attendance")
public class add_attendance extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

        String teacher_id = request.getParameter("teacher_id");
        String course_id = request.getParameter("course_id");
        String attendance_time = request.getParameter("attendance_time");
        String status = "到课";

        try{
            CourseDao courseDao = new CourseDao();
            AttendanceDao attendanceDao = new AttendanceDao();
            List<String> stu_ids = courseDao.getStuIdsByCourseAndTeacherId(course_id,teacher_id);
            for(String stu_id: stu_ids){
                attendanceDao.addAttendance(stu_id, course_id, attendance_time,teacher_id, status);
            }
            session.setAttribute("teacher_id", teacher_id);
            response.sendRedirect("one_page_attendance");
        }catch (Exception e){
            out.print(e);
        }



    }

}
